package com.kaiyiweitong.tongxing.activity.my;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.TextView;

import com.kaiyiweitong.tongxing.R;
import com.kaiyiweitong.tongxing.base.BaseActivity;
import com.kaiyiweitong.tongxing.fragment.my_coupon.PlatformCouponFragment;
import com.kaiyiweitong.tongxing.fragment.my_coupon.StoreCouponFragment;
import com.kaiyiweitong.tongxing.other.FragmentFactory;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Administrator on 2017/12/17 0017.
 */

public class MyCouponActivity extends BaseActivity implements ViewPager.OnPageChangeListener {
    @Bind(R.id.platform_coupon)
    TextView mPlatformCoupon;
    @Bind(R.id.platform_coupon_line)
    View mPlatformCouponLine;
    @Bind(R.id.store_coupon)
    TextView mStoreCoupon;
    @Bind(R.id.store_coupon_line)
    View mStoreCouponLine;
    @Bind(R.id.vp_my_coupon)
    ViewPager mVpMyCoupon;

    private List<Fragment> mFragments = new ArrayList<>();

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_coupon);
        ButterKnife.bind(this);
        init();
    }

    private void init() {
        mFragments.add(FragmentFactory.getInstance(PlatformCouponFragment.class));
        mFragments.add(FragmentFactory.getInstance(StoreCouponFragment.class));
        FragmentPagerAdapter adapter = new FragmentPagerAdapter(getSupportFragmentManager()) {
            @Override
            public int getCount() {
                return mFragments.size();
            }

            @Override
            public Fragment getItem(int position) {
                return mFragments.get(position);
            }
        };
        mVpMyCoupon.setAdapter(adapter);
        mVpMyCoupon.addOnPageChangeListener(this);
        indicator(0);
    }

    @OnClick({R.id.platform_coupon, R.id.store_coupon})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.platform_coupon:
                indicator(0);
                break;
            case R.id.store_coupon:
                indicator(1);
                break;
        }
    }

    private int currentIndex = -1;

    private synchronized void indicator(int position) {
        if (currentIndex != position) {
            mVpMyCoupon.setCurrentItem(position);
            currentIndex = position;
            if (position == 0) {
                mPlatformCoupon.setSelected(true);
                mStoreCoupon.setSelected(false);
                mPlatformCouponLine.setVisibility(View.VISIBLE);
                mStoreCouponLine.setVisibility(View.INVISIBLE);
            } else {
                mPlatformCoupon.setSelected(false);
                mStoreCoupon.setSelected(true);
                mPlatformCouponLine.setVisibility(View.INVISIBLE);
                mStoreCouponLine.setVisibility(View.VISIBLE);
            }
        }
    }

    @Override
    public void onPageSelected(int position) {
        indicator(position);
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }
}